
package cn.dy.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;

import org.apache.commons.io.FileUtils;

/**
 * @Description:
 * @Date：2018年6月29日 @author： DongY
 * @version ：1.0 1.[2018年6月29日]CreatBy：dy
 */

public class FileUtil {
	/**
	 * 根据路径读取文件
	 * 
	 * @Description:
	 * @param filePath
	 * @return
	 * @throws IOException
	 *             String
	 */
	public static String readAsString(final String filePath) throws IOException {
		// System.out.println(filePath);
		// final URL url = FileUtil.class.getResource(filePath);
		// final String context = FileUtils.readFileToString(new
		// File(url.toString()), Charset.forName("utf-8"));
		System.err.println(filePath);
		final InputStream inputStream = FileUtil.class.getClassLoader().getResourceAsStream(filePath);
		final BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
		String context = "";
		String line = "";
		while ((line = br.readLine()) != null) {
			context += line + "\r\n";
		}
		return context;
	}

	public static void write(final String context, final String outPath) throws IOException {
		final File file = new File(outPath);
		FileUtils.writeStringToFile(file, context, Charset.defaultCharset());
	}

	public static void main(final String[] args) throws IOException {
		final String string = "hello word";
		write(string, "d://hhhhhhhhhhhhhhhhhh/h.java");
	}
}
